package com.yvan.codeslave.snippet;

import com.google.common.collect.Maps;
import com.yvan.Consts;
import com.yvan.codeslave.BeetlUtils;
import com.yvan.codeslave.PluginUtils;
import com.yvan.platform.FileUtils;
import com.yvan.platform.JsonWapper;
import lombok.SneakyThrows;
import lombok.val;
import org.apache.commons.io.IOUtils;
import org.beetl.core.GroupTemplate;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class SnippetService {

    private static boolean loaded = false;
    private static boolean loadSuccess = false;
    private static Map<String, String> templates;
    private static GroupTemplate mapGroupTemplate;

    @SneakyThrows
    public static void reload() {
        val state = PluginUtils.getState();
        if (state == null) {
            Idea.showError("请设置好 codeslave 的主目录");
            loadSuccess = false;
            return;
        }

        val file = FileUtils.getResource(state.basePath, Consts.SNIPPET);
        if (!file.exists()) {
            Idea.showError(file.getCanonicalPath() + " 文件不存在!");
            loadSuccess = false;
            return;
        }

        List<String> lines;
        try (InputStream is = new FileInputStream(file)) {
            lines = IOUtils.readLines(is, "UTF-8");
        }

        JsonWapper jwMenu = new JsonWapper();
        boolean codon = false;
        StringBuilder sb = new StringBuilder();
        sb.append("<%\r\n");
        String menuName = "";
        boolean isJs = false;

        for (String line : lines) {
            if (codon) {
                //在代码范围内
                if (line.startsWith("```")) {
                    //代码结束
                    codon = false;
                    if (isJs) {
                        sb.append("\r\n%>");
                    }
                    jwMenu.set(menuName, sb.toString());
                    menuName = "";

                } else {
                    //代码未结束，一直往后累加
                    sb.append(line).append("\r\n");
                }
            } else {

                //还没进入代码范围
                if (line.startsWith("# ")) {
                    menuName = line.substring(2).trim();
                }
                if (line.startsWith("```javascript")) {
                    codon = true;
                    sb = new StringBuilder();
                    sb.append("<%\r\n");
                    isJs = true;

                } else if (line.startsWith("```")) {
                    codon = true;
                    sb = new StringBuilder();
                    isJs = false;
                }

            }

        }

        templates = Maps.newLinkedHashMap();
        for (String menu : jwMenu.keys()) {
            templates.put(menu, jwMenu.asStr(menu));
        }
        loadSuccess = true;
        mapGroupTemplate = BeetlUtils.createSnippetGroupTemplate(templates);
    }

    public static GroupTemplate getMapGroupTemplate() {
        return mapGroupTemplate;
    }

    public synchronized static Map<String, String> getTemplates() {
        if (!loaded) {
            reload();
            loaded = true;
        }
        return templates;
    }
}
